<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
    <link
            href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css"
            rel="stylesheet"
    />
    <style>
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100; /* 按需定义 */
        }
        #toolbar-container {
            border-bottom: 1px solid #ccc;
        }
        #editor-container {
            height: 500px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">工单标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属小区</label>
        <div class="layui-input-block">
    <select name="communityId">
        <option value="">请选择</option>
    </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">工单分类</label>
        <div class="layui-input-block">
        <select name="orderType">
            <option value="">请选择</option>
        </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">报修人</label>
        <div class="layui-input-block">
            <input type="text" name="submitter" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">保修手机号</label>
        <div class="layui-input-block">
            <input type="text" name="submitterPhone" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
<%--    富文本编辑器--%>
    <div id="editor—wrapper">
        <div id="toolbar-container"><!-- 工具栏 --></div>
        <div id="editor-container"><!-- 编辑器 --></div>
    </div>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="ID-upload-demo-btn-2">
            <i class="layui-icon layui-icon-upload"></i> 多图片上传
        </button>
        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 11px;">
            预览图：
            <div class="layui-upload-list" id="upload-demo-preview"></div>
        </blockquote>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script>
    const { createEditor, createToolbar } = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            // 也可以同步到 <textarea>
        },
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })
</script>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        var $ = layui.$;
        var upload = layui.upload;

        let uploadImagesPath="";
        // 多图片上传
        upload.render({
            elem: '#ID-upload-demo-btn-2',
            url: '/file/uploadImages.do', // 实际使用时改成您自己的上传接口即可。
            multiple: true,
            accept: 'file',
            exts: 'jpg|jpeg|png|gif|bmp',
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#upload-demo-preview').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 90px; height: 90px;">')
                });
            },
            done: function(res){
                // 上传完毕
                console.log(res.data)
                uploadImagesPath+=","+res.data;
            }
        });
        //渲染社区下拉框
        $.post("/order/listCommunity.do",function (res){
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                $("select[name='communityId']").append("<option value='"+data[i].id+"'>"+data[i].communityName+"</option>")
            }
            // 渲染表单
            form.render('select')
        })

        //渲染社区下拉框
        $.post("/order/listType.do",{'type':'工单类型'},function (res){
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                $("select[name='orderType']").append("<option value='"+data[i].code+"'>"+data[i].name+"</option>")
            }
            // 渲染表单
            form.render('select')
        })


        // 提交事件
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获取表单字段值
            //获取富文本编辑器html内容
            const html = editor.getHtml();
            //封装到表单对象中
            field.content=html;
            field.images = uploadImagesPath.substring(1);
            console.log(field)
            $.post("/order/add.do",field,function (res){
                if(res.code==0){
                    layer.msg(res.msg,{time:2000},function (){
                        window.location.href="/order/tolist.do"
                    })
                }else{
                    layer.msg(res.msg,{time:2000})
                }
            })
            return false
        });
        // 日期
        laydate.render({
            elem: '#date'
        });

    });
</script>

</body>
</html>